package com.pxx.service.service;

import com.pxx.item.pojo.Category;
import com.pxx.service.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;


    public List<Category> queryCategoryList(Long pid) {
        //因为我们引入了 tk.mapper
        //如果你new 一个对象。 并且对对象中的某一个字段或者多个字段赋值
        //然后你执行select方法的时候， 他会通过你传入的对象中的值或者多个值，进行and拼接
        // select * from tb_category where parend_id = ?
        Category category = new Category();
        category.setParentId(pid);

        return categoryMapper.select(category);
    }

    public List<String> queryNamesByIds(List<Long> cids) {
    List<String> list =     new ArrayList<String>();
        for (Long cid : cids) {
            Category category = categoryMapper.selectByPrimaryKey(cid);
            list.add(category.getName()) ;
        }

        return list;
    }

    public List<Category> queryAllByCid3(Long id) {
        Category c3 = this.categoryMapper.selectByPrimaryKey(id);
        Category c2 = this.categoryMapper.selectByPrimaryKey(c3.getParentId());
        Category c1 = this.categoryMapper.selectByPrimaryKey(c2.getParentId());
        return Arrays.asList(c1,c2,c3);
    }
}
